﻿using System.Collections.Generic;
using Game.NetWork;
using GameEntitySystem;

namespace Game
{
    public class ComponentXiaoshiPackage : IPackage
    {
        private struct FurnacePackageData
        {
            public float fireTimeRemaining;

            public float smeltingProgress;

            public float heatLevel;

            public ushort entityID;
        }

        private List<FurnacePackageData> furnaceDataList = new List<FurnacePackageData>();

        public byte ID => 105;

        public Client To { get; set; }

        public Client Except { get; set; }

        public Client From { get; set; }

        public ClientState MinNeedState => ClientState.ProjectLoaded;

        public ComponentXiaoshiPackage()
        {
        }

        public ComponentXiaoshiPackage(List<FCComponentFermentation> componentFurnaces)
        {
            foreach (FCComponentFermentation componentFurnace in componentFurnaces)
            {
                FurnacePackageData item = new FurnacePackageData
                {
                    entityID = componentFurnace.Entity.EntityId,
                    smeltingProgress = componentFurnace.m_smeltingProgress
                };
                furnaceDataList.Add(item);
            }
        }

        public ComponentXiaoshiPackage(ushort entityId, float smeltingProgress)
        {
            FurnacePackageData item = new FurnacePackageData
            {
                entityID = entityId,
                smeltingProgress = smeltingProgress
            };
            furnaceDataList.Add(item);
        }

        public void Handle(ProjectNet projectNet, NetNode netNode, bool isServer)
        {
            foreach (FurnacePackageData furnaceData in furnaceDataList)
            {
                projectNet.FindEntityById(furnaceData.entityID, delegate (Entity e)
                {
                    FCComponentFermentation fCComponentFermentation = e.FindComponent<FCComponentFermentation>();
                    fCComponentFermentation.m_smeltingProgress = furnaceData.smeltingProgress;
                });
            }
        }

        public void ReadData(PackageStreamReader reader)
        {
            int num = reader.ReadInt32();
            for (int i = 0; i < num; i++)
            {
                FurnacePackageData item = default(FurnacePackageData);
                item.entityID = reader.ReadUInt16();
                item.smeltingProgress = reader.ReadSingle();
                furnaceDataList.Add(item);
            }
        }

        public void WriteData(PackageStreamWriter writer)
        {
            writer.Write(furnaceDataList.Count);
            foreach (FurnacePackageData furnaceData in furnaceDataList)
            {
                writer.Write(furnaceData.entityID);
                writer.Write(furnaceData.smeltingProgress);
            }
        }
    }
}